/*global define, console */

define(['angular','store'], function (angular, store) {
    "use strict";

    var factory = function ($location, $http, CacheFactory) {

        var storeWithExpiration = {
            set: function(key, val, exp) {
                store.set(key, { val:val, exp:exp, time:new Date().getTime() })
            },
            get: function(key) {
                var info = store.get(key);
                if (!info) { return null }
                if (new Date().getTime() - info.time > info.exp) { return null }
                return info.val
            }
        };

        CacheFactory('UserCache', {
            deleteOnExpire: 'aggressive',
            maxAge: 3600000,
            storageMode: 'sessionStorage',
            storagePrefix: "tfzq."
        });

        return {
            setAppContextName : function(appContextName) {
                 if(appContextName) {
                    store.set('appContextName', appContextName);
                }
            },
             getAppContextName : function(){ //渠道号获取
                var appContextName = store.get('appContextName');
                if(appContextName && appContextName != "" && appContextName != "undefined") {
                    return appContextName;
                }
                return "";
            },
            setRecommendInfos : function(recommendInfos){  //渠道号设置
                if(recommendInfos) {
                    if(recommendInfos.length > 5) {
                        recommendInfos = recommendInfos.substr(0, 5);
                    }
                    store.set('recommendInfos', recommendInfos);
                }
            },
            getRecommendInfos : function(){ //渠道号获取
                var recommendInfos = store.get('recommendInfos');
                if(recommendInfos && recommendInfos != "" && recommendInfos != "undefined") {
                    return recommendInfos;
                }
                return "";
            },
            setUser : function(user){ //保存用户信息
                var userCache = CacheFactory.get('UserCache');
                var old_user = userCache.get('user');
                if(null != old_user){
                    user.token = old_user.token;
                }
                userCache.put('user', user);
            },
            getUser : function(){ //获取用户信息
                var userCache = CacheFactory.get('UserCache');
                return userCache.get('user');
            },
            removeUser : function() { //删除用户信息
                var userCache = CacheFactory.get('UserCache');
                userCache.remove('user');
            },
            setOpenUser : function(user){
                store.set('openUser', user);
            },
            getOpenUser : function(){
                return store.get('openUser');
            },
            removeOpenUser : function() {
                store.remove('openUser');
            },
            setAccount : function(account){
                store.set('account', account);
            },
            getAccount : function(){
                return store.get('account');
            },
            removeAccount : function() {
                store.remove('account');
            },
            getAccountList: function(){
                var accountList = store.get('accountList');
                if(accountList && accountList != "" && accountList != "undefined") {
                    return accountList;
                }
                return "";
            },
            setAccountList: function(accountList){
                store.set('accountList',accountList);
            },
            getRememberAccount : function(){
                return store.get('rememberAccount');
            },
            setRememberAccount : function(rememberAccount){
                return store.set('rememberAccount', rememberAccount);
            },
            set : function(key, value) {
                store.set(key, value);
            },
            get : function(key) {
                return store.get(key);
            },
            remove : function(key) {
                store.remove(key)
            }
        }
    };

    factory.$inject = ['$location', '$http', 'CacheFactory'];
    return factory;
});